#include <iostream>
#include <cstdio>
#include <stack>
using namespace std;

stack<int> data_stack;

int main(int argc, char const *argv[]){
    int n;
    while (scanf("%d", &n) != EOF){
        if (n == 0)
            break;
        char input_char;
        int push_num;
        for (int i = 0; i < n; i++){
            cin >> input_char;
            if (input_char == 'P'){
                cin >> push_num;
                data_stack.push(push_num);
            } else if (input_char == 'A'){
                if (!data_stack.empty())
                    printf("%d\n", data_stack.top());
                else
                    printf("E\n");
            } else {
                if (!data_stack.empty())
                    data_stack.pop();
            }
        }
        printf("\n");
    }
    return 0;
}
